using System;
using System.Net;
using Dimebrain.TweetSharp.Core.Caching;

namespace Dimebrain.TweetSharp.Core.Web.Query
{
    partial class WebQueryBase
    {
        protected virtual IAsyncResult ExecutePostAsync(string url, string key, IClientCache cache)
        {
            Response = null;

            byte[] content;
            var request = BuildPostWebRequest(url, out content);

            var state = new Pair<WebRequest, Triplet<byte[], IClientCache, string>>
                        {
                            First = request,
                            Second = new Triplet<byte[], IClientCache, string>
                                     {
                                         First = content,
                                         Second = cache,
                                         Third = key
                                     }
                        };
            
            var args = new WebQueryRequestEventArgs(url);
            OnQueryRequest(args);

            return request.BeginGetRequestStream(PostAsyncRequestCallback, state);
        }

        protected virtual IAsyncResult ExecutePostAsync(string url, string prefixKey, IClientCache cache, DateTime absoluteExpiration)
        {
            Response = null;

            byte[] content;
            var request = BuildPostWebRequest(url, out content);

            var state = new Pair<WebRequest, Pair<byte[], Triplet<IClientCache, DateTime, string>>>
                        {
                            First = request,
                            Second = new Pair<byte[], Triplet<IClientCache, DateTime, string>>
                                     {
                                         First = content,
                                         Second = new Triplet<IClientCache, DateTime, string>
                                                  {
                                                      First = cache,
                                                      Second = absoluteExpiration,
                                                      Third = prefixKey
                                                  }
                                     }
                        };

            var args = new WebQueryRequestEventArgs(url);
            OnQueryRequest(args);

            return request.BeginGetRequestStream(PostAsyncRequestCallback, state);
        }

        protected virtual IAsyncResult ExecutePostAsync(string url, string prefixKey, IClientCache cache, TimeSpan slidingExpiration)
        {
            Response = null;

            byte[] content;
            var request = BuildPostWebRequest(url, out content);

            var state = new Pair<WebRequest, Pair<byte[], Triplet<IClientCache, TimeSpan, string>>>
                        {
                            First = request,
                            Second = new Pair<byte[], Triplet<IClientCache, TimeSpan, string>>
                                     {
                                         First = content,
                                         Second = new Triplet<IClientCache, TimeSpan, string>
                                                  {
                                                      First = cache,
                                                      Second = slidingExpiration,
                                                      Third = prefixKey
                                                  }
                                     }
                        };

            var args = new WebQueryRequestEventArgs(url);
            OnQueryRequest(args);

            return request.BeginGetRequestStream(PostAsyncRequestCallback, state);
        }
    }
}